<?php
// put all data operation to here. 
//Scripts functions from web page will directly invoke function from this file.

require_once 'MJDMTask.php';
require_once 'MJDMUser.php';
require_once 'MJDMReward.php';
require_once 'common.php';
require_once 'db_utility.php';

log_action("In Page : ".__FILE__);

//reload all data from databases when page first loaded
function ReloadTaskData()
{
	log_action("Call Funtion : " .__FUNCTION__);
	$uid = GetSessionUID();
	
	unset($_SESSION['user'][$uid]);
	LoadUserData($uid);
	
	unset($_SESSION['task'][$uid]);
	LoadTaskData($uid);
}

function ReloadRewardData()
{
	log_action("Call Funtion : " .__FUNCTION__);
	$uid = GetSessionUID();
	
	unset($_SESSION['user'][$uid]);
	LoadUserData($uid);
	
	unset($_SESSION['reward'][$uid]);
	LoadRewardData($uid);
}

function GetUserBasicInfo()
{
	log_action("Call Funtion : " .__FUNCTION__);
	$uid = GetSessionUID();
	
	if(isset($_SESSION['user'][$uid]))
	{
		return $_SESSION['user'][$uid];
	}
	else
	{
		return LoadUserData($uid);
	}
}

function SetUserBasicInfo($user)
{
	log_action("Call Funtion : " .__FUNCTION__);
	$uid = GetSessionUID();
	$_SESSION['user'][$uid] = $user;
}

function UpdateUserPoints($points)
{
	log_action("Call Funtion : " .__FUNCTION__);
	$user = GetUserBasicInfo();
	
	if(DB_UpdateUserPoints($user->id, $points))
	{
		$user->earnPoints = $user->earnPoints + $points;
		SetUserBasicInfo($user);
	}
}

function GetUserTasksInfo()
{
	log_action("Call Funtion : " .__FUNCTION__);
	$uid = GetSessionUID();
	
	if(isset($_SESSION['task'][$uid]))
	{
		return $_SESSION['task'][$uid];
	}
	else
	{
		return LoadTaskData($uid);
	}
}

function SetUserTasksInfo($tasklist)
{
	log_action("Call Funtion : " .__FUNCTION__);
	$uid = GetSessionUID();
	$_SESSION['task'][$uid] = $tasklist;
}

function GetUserRewardsInfo()
{
	log_action("Call Funtion : " .__FUNCTION__);
	$uid = GetSessionUID();
	
	if(isset($_SESSION['reward'][$uid]))
	{
		return $_SESSION['reward'][$uid];
	}
	else
	{
		return LoadRewardData($uid);
	}
}

function SetUserRewardsInfo($rewardlist)
{
	log_action("Call Funtion : " .__FUNCTION__);
	$uid = GetSessionUID();
	$_SESSION['reward'][$uid] = $rewardlist;
}

//load data after login
function LoadUserData($uid)
{
	log_action("Call Funtion : " .__FUNCTION__);
	$user = new User();
	
	$row = DB_FetchUserRow($uid);
	
	$user->id = $uid;
	$user->displayName = $row["displayName"];
	
	//current point
	$user->earnPoints = $row["earnPoints"];
	
	//save user session;
	$_SESSION['user'][$uid] = $user;
	
	return $user;

}

//load user tasks data
function LoadTaskData($uid)
{
	log_action("Call Funtion : " .__FUNCTION__);
	$tasklist = new MyTaskList();
	
	$result = DB_FetchTaskList($uid);
	
	while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
		$task = new Task();
		
		$task->id = $line['id'];
		$task->name = $line['name'];
		$task->points = $line['points'];
		$task->dueDate = $line['dueDate'];
		$task->tag = $line['tag'];
		$task->url = $line['url'];
		
		$tasklist->Add($task); 
	}
	
	$_SESSION['task'][$uid] = $tasklist;
	return $tasklist;
}

//load user rewards data
function LoadRewardData($uid)
{
	log_action("Call Funtion : " .__FUNCTION__);
	$rewardlist = new MyRewardList();

	$result = DB_FetchRewardList($uid);

	while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
		$reward = new Reward();

		$reward->id = $line['id'];
		$reward->name = $line['name'];
		$reward->points = $line['points'];
		$reward->url = $line['url'];

		$rewardlist->Add($reward);
	}

	$_SESSION['reward'][$uid] = $rewardlist;
	return $rewardlist;
}

//add a task
function AddTask($task)
{
	log_action("Call Funtion : " .__FUNCTION__);
	$tasklist = GetUserTasksInfo();

	//var_dump($tasklist);

	if(DB_InsertTask($task))
	{
		$tasklist->Add($task);
		SetUserTasksInfo($tasklist);
	}
}

//delete a task
function DeleteTask($id)
{
	log_action("Call Funtion : " .__FUNCTION__);
	$tasklist = GetUserTasksInfo();
	if(DB_DeleteTask($id))
	{
		$tasklist->Delete($id);
		SetUserTasksInfo($tasklist);
	}
}

function UpdateTask($task)
{
	log_action("Call Funtion : " .__FUNCTION__);
	$tasklist = GetUserTasksInfo();

	if(DB_UpdateTask($task))
	{
		$tasklist->Update($task);
		SetUserTasksInfo($tasklist);
	}
}


//finsih a task
function FinishTask($id,$points)
{
	log_action("Call Funtion : " .__FUNCTION__);
	$tasklist = GetUserTasksInfo();
	
	if(DB_FinishTask($id))
	{
		$tasklist->Delete($id);
		SetUserTasksInfo($tasklist);
	}
	
	UpdateUserPoints($points);
}

function AddReward($reward)
{
	log_action("Call Funtion : " .__FUNCTION__);
	$rewardlist = GetUserRewardsInfo();
	
	if(DB_InsertReward($reward))
	{
		$rewardlist->Add($reward);
		SetUserRewardsInfo($rewardlist);
	}
}

function UpdateReward($reward)
{
	log_action("Call Funtion : " .__FUNCTION__);
	$rewardlist = GetUserRewardsInfo();
	
	if(DB_Updatereward($reward))
	{
		$rewardlist->Update($reward);
		SetUserRewardsInfo($rewardlist);
	}
}

function DeleteReward($id)
{
	log_action("Call Funtion : " .__FUNCTION__);
	$rewardlist = GetUserRewardsInfo();
	
	if(DB_DeleteReward($id))
	{
		$rewardlist->Delete($id);
		SetUserRewardsInfo($rewardlist);
	}
}

function WonReward($id,$points)
{
	log_action("Call Funtion : " .__FUNCTION__);
	$rewardlist = GetUserRewardsInfo();
	
	if(DB_WonReward($id,$points))
	{
		$rewardlist->Delete($id);
		SetUserRewardsInfo($rewardlist);
	}
	
	UpdateUserPoints(-$points);
}
?>
 